package com.devframe.srtp.client.handle;

import java.io.File;
import java.net.URI;

import org.jboss.netty.handler.codec.http.multipart.HttpDataFactory;
import org.jboss.netty.handler.codec.http.multipart.HttpPostRequestEncoder;
import org.jboss.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException;

import com.devframe.srtp.SRTPConstants;
import com.devframe.srtp.util.enumobj.EnumSRTPAction;
/**
 * 上传文件
 * @author 詹保山
 *
 */
public class ClientUploadFileHandler extends ClientWrapFileHandler{
	private File file;
	private String thumbMark=SRTPConstants.THUMB_MARK_NO;
	private String fileName;
	
	public ClientUploadFileHandler(String host,URI uri,File file,String fileName,String thumbMark,String userName,String pwd){
		super(host,uri,userName,pwd);
		this.file=file;
		this.thumbMark=thumbMark;
		this.fileName=fileName;
	}

	public HttpPostRequestEncoder wrapRequestData(HttpDataFactory factory){
        HttpPostRequestEncoder bodyRequestEncoder = null;
        try {
            bodyRequestEncoder = new HttpPostRequestEncoder(factory, this.getRequest(), true); // true => multipart
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (ErrorDataEncoderException e) {
            e.printStackTrace();
        }
        //设置参数
        try {
            bodyRequestEncoder.addBodyAttribute("getform", "POST");
            bodyRequestEncoder.addBodyAttribute(SRTPConstants.ACTION_KEY, EnumSRTPAction.UPLOAD_FILE.getValue());
            bodyRequestEncoder.addBodyAttribute(SRTPConstants.THUMB_MARK_KEY, thumbMark);
            bodyRequestEncoder.addBodyAttribute(SRTPConstants.USER_NAME_KEY, super.getUserName());
            bodyRequestEncoder.addBodyAttribute(SRTPConstants.PWD_KEY, super.getPwd());
            bodyRequestEncoder.addBodyAttribute(SRTPConstants.FILE_NAME_KEY, this.fileName);
            bodyRequestEncoder.addBodyFileUpload("myfile", file, "application/x-zip-compressed", false);
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (ErrorDataEncoderException e) {
            e.printStackTrace();
        }
        try {
            bodyRequestEncoder.finalizeRequest();
        } catch (ErrorDataEncoderException e) {
            e.printStackTrace();
        }
		return bodyRequestEncoder;
	}
}
